//
//  ByInformationNoticeVC.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/19.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByInformationNoticeVC.h"

/** 单元格 */
#import "ByNiticeCell.h"                // 公告cell

/** vm */
#import "ByInformationViewModel.h"      // vm

/** 数据 */
#import "ByNetNewsModel.h"              // news model

@interface ByInformationNoticeVC () <UITableViewDelegate, UITableViewDataSource, ByInformationPostQueryDelegate>
{
    NSInteger _page;        
}
@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) ByInformationViewModel *vm;
@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation ByInformationNoticeVC

#pragma mark - getter
- (NSMutableArray *)items {
    if (!_items) {
        _items = [[NSMutableArray alloc] init];
    }
    return _items;
}
- (ByInformationViewModel *)vm {
    if (!_vm) {
        _vm = [[ByInformationViewModel alloc] init];
        _vm.postDelegate = self;
    }
    return _vm;
}

#pragma mark - viewcontroller method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"通知公告";
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    
    [self createLeftBackBarButtonItem];
    
    [self createTableView];
    
    [self.table.mj_header beginRefreshing];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
        
    ByTabBarController *tabBar = (ByTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    [tabBar hideTabBar];
}


#pragma mark - Net request
- (void)postQuery {
    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
    params.cate = [NSString stringWithFormat:@"%li", NEWS_TYPE_FOR_NOTICE];
    params.page_num = [NSString stringWithFormat:@"%li", _page];
    params.page_size = @"5";
    [self.vm postQuery:params];
}
- (void)postQuerySuccess:(ByNetBaseResult *)result {
    
    ByNetNewsModel *model = result.data;
//    ARRAR_LOG(model.results);
    
    if (_page == 1) {
        [self.items removeAllObjects];
        [self.items addObjectsFromArray:model.results];
        [self.table.mj_footer resetNoMoreData];
    } else {
        [self.items addObjectsFromArray:model.results];
    }
    
    [self.table reloadData];
    if (self.items.count == model.total.integerValue) {
        [self.table.mj_footer endRefreshingWithNoMoreData];
        [self.table.mj_header endRefreshing];
        return;
    }
    [self.table.mj_footer endRefreshing];
    [self.table.mj_header endRefreshing];
}
- (void)postQueryFailure:(ByNetBaseResult *)result {
    [self.table.mj_footer endRefreshing];
    [self.table.mj_header endRefreshing];
    [ByCoreToolAlert alertError:result.msg];
}

#pragma mark - create view
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    self.table.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    ByCoreRefreshGifFooter *footer = [ByCoreRefreshGifFooter footerWithRefreshingBlock:^{
        _page ++;
        [self postQuery];
    }];
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self postQuery];
    }];
    self.table.mj_footer = footer;
    self.table.mj_header = header;
}


#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.items.count;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ByNiticeCell *cell = [ByNiticeCell createCellInTableview:tableView];
    cell.model = self.items[indexPath.row];
    return cell;
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 310;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    ByNetNewsItemModel *model = self.items[indexPath.row];
    NSString *postID = model.id;
    NSString *urlStr = [NSString stringWithFormat:@"Webview/Post/viewPost?id=%@", postID];
    ByWebViewController *vc = [[ByWebViewController alloc] initWithUrlString:urlStr];
    vc.navigationItem.title = model.post_cate_name;
    [self.navigationController pushViewController:vc animated:YES];

}

@end
